<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${basicPath}">

    <resultMap id="${model.varName}Map" type="${model.basicPath}">
        <#list model.fields as field>
        <#if field.id>
        <id property="${field.name}" column="${field.columnName}"/>
        <#else>
        <result property="${field.name}" column="${field.columnName}"/>
        </#if>
        </#list>
    </resultMap>

<#list model.fields as field>
    <#if field.id>
    <!--新增${model.comment}-->
    <insert id="insert">
        INSERT INTO
        ${model.tableName}(<#list model.fields as field><#if field_index!=0>, </#if>${field.columnName}</#list>)
        VALUES
        (<#list model.fields as field><#if field_index!=0>, </#if><#noparse>#{</#noparse>${field.name}}</#list>)
    </insert>

    <!--通过ID查询单个${model.comment}-->
    <select id="findById" resultMap="${model.varName}Map">
        SELECT
        <#list model.fields as field><#if field_index!=0>, </#if>${field.columnName}</#list>
        FROM ${model.tableName}
        WHERE
        <#list model.fields as field><#if field.id>${field.columnName} = <#noparse>#{</#noparse>${field.name}}</#if></#list>
    </select>

    <!--修改${model.comment}-->
    <update id="update">
        UPDATE ${model.tableName}
        SET
        <#list model.fields as field><#if !field.id><#if model.fields[0].id&&field_index!=1>, </#if>${field.columnName} = <#noparse>#{</#noparse>${field.name}}</#if></#list>
        WHERE
        <#list model.fields as field><#if field.id>${field.columnName} = <#noparse>#{</#noparse>${field.name}}</#if></#list>
    </update>

    <!--通过ID删除单个${model.comment}-->
    <delete id="deleteById">
        DELETE
        FROM ${model.tableName}
        WHERE
        <#list model.fields as field><#if field.id>${field.columnName} = <#noparse>#{</#noparse>${field.name}}</#if></#list>
    </delete>
    </#if>
</#list>

    <!--查询${model.comment}列表-->
    <select id="findList" resultMap="${model.varName}Map">
        SELECT
        <#list model.fields as field><#if field_index!=0>, </#if>${field.columnName}</#list>
        FROM ${model.tableName}
        <#noparse><where></#noparse><#list model.fields as field>
        <#noparse>    <if test="</#noparse>${field.name} != null<#if field.typeSimpleName=='String'> and ${field.name} != ''</#if><#noparse>"></#noparse>
                AND ${field.columnName} = <#noparse>#{</#noparse>${field.name}}
        <#noparse>    </if></#noparse></#list>
        <#noparse></where></#noparse>
    </select>

    <!--查询${model.comment}数据条数-->
    <select id="findCount" resultType="int">
        SELECT
        COUNT(*) as count
        FROM ${model.tableName}
        <#noparse><where></#noparse><#list model.fields as field>
        <#noparse>    <if test="</#noparse>${field.name} != null<#if field.typeSimpleName=='String'> and ${field.name} != ''</#if><#noparse>"></#noparse>
                AND ${field.columnName} = <#noparse>#{</#noparse>${field.name}}
        <#noparse>    </if></#noparse></#list>
        <#noparse></where></#noparse>
    </select>

    <!--分页查询${model.comment}列表-->
    <select id="findPageList" resultMap="${model.varName}Map">
        SELECT
        <#list model.fields as field><#if field_index!=0>, </#if>${field.columnName}</#list>
        FROM ${model.tableName}
        <#noparse><where></#noparse><#list model.fields as field>
        <#noparse>    <if test="</#noparse>${field.name} != null<#if field.typeSimpleName=='String'> and ${field.name} != ''</#if><#noparse>"></#noparse>
                AND ${field.columnName} = <#noparse>#{</#noparse>${field.name}}
        <#noparse>    </if></#noparse></#list>
            LIMIT <#noparse>#{(pageANum-1)*pageASize}</#noparse>, <#noparse>#{pageASize}</#noparse>
        <#noparse></where></#noparse>
    </select>
</mapper>